<?php
/**
 * 店铺API
 */
namespace app\ais\controller\api\v1;
use app\ais\controller\api\Base;
use app\ais\model\AisStore;
use app\ais\model\AisStoreCate;
use app\ais\model\AisCoupon;
use app\ais\model\AisShop;
use think\helper\Time;
use filter\Filter;

class Store extends Base{

    
    /**
     * 读取行业信息列表  v4.1
     * @param integer 读取ID
     * @return json
     */
    public function cate(){
        $param['cate_id'] = $this->request->param('cate_id/d',0);
        $this->apiSign($param);
        $where['parent_id']         = $param['cate_id'];
        $where['member_miniapp_id'] = $this->miniapp_id;
        $data = AisStoreCate::where($where)->field('id,name,picture,title')->order('sort desc,id desc')->select();
        if($data->isEmpty()){
            return enjson(204);
        }
        return enjson(200,$data);
    }

    /**
     * 店铺列表
     */
    public function index(){
        $param['keyword']  = $this->request->param('keyword');
        $param['cate_id']  = $this->request->param('cate_id',0);
        $param['cate_sid'] = $this->request->param('cate_sid/d',0);
        $param['page']     = $this->request->param('page/d',0);
        $param['citycode'] = $this->request->param('citycode/d');
        $this->apiSign($param);
        $where = [];
        if($param['cate_sid']){
            $where[] = ['cate_sid','=',$param['cate_sid']];
        }
        if($param['cate_id']){
            $where[] = ['cate_id','=',$param['cate_id']];
        }
        $where[] = ['member_miniapp_id','=',$this->miniapp_id];
        $where[] = ['is_lock','=',0];
        if($param['citycode']){
            $where[] = ['citycode','=',$param['citycode']];
        }
        $rel = AisStore::where($where)->withSearch(['name'],['name' => $param['keyword']])
            ->field('id,name,longitude,latitude,img,telphone,address,tags,is_top')
            ->order('is_top desc,sort desc,id desc')->page($param['page'],10)->select();
        if($rel->isEmpty()){
            return enjson(204,'没有内容');
        }
        return enjson(200,$rel);
    }
   
    /**
     * 店铺首页产品
     */
    public function read(){
        $param['store_id'] = $this->request->param('store_id/d',0);
        $this->apiSign($param);
        $info = AisStore::with(['chain'])->where(['id' => $param['store_id'],'is_lock' => 0])->field('id,name,longitude,latitude,img,telphone,address,tags,is_top,likes')->find();
        if(empty($info)){
            return enjson(404);
        }
        return enjson(200,$info);
    }

    /**
     * 点赞
     **/
    public function like(){
        $this->isUserAuth();
        $param['store_id']   = $this->request->param('store_id/d',0);
        $this->apiSign($param);
        $rel = AisStore::where(['id' => $param['store_id']])->setInc('likes',1);
        if(empty($rel)){
            return enjson(204);
        }
        return enjson(200);
    }

     /**
     * 申请入驻商家
     */
    public function reg(){
        $this->isUserAuth();
        if (request()->isPost()) {
            $param = [
                'name'      => $this->request->param('name/s'),
                'address'   => $this->request->param('address/s'),
                'telphone'  => $this->request->param('telphone/s'),
                'latitude'  => $this->request->param('latitude/s'),
                'longitude' => $this->request->param('longitude/s'),
                'charge'    => $this->request->param('charge/d',10),
                'citypath'  => $this->request->param('citypath/s','[]','htmlspecialchars_decode'),
                'imgs'      => $this->request->param('imgs/s','[]','htmlspecialchars_decode'),
            ];
            $this->apiSign($param);
            $validate = $this->validate($param,'Store.reg');
            if(true !== $validate){
                return enjson(403,$validate);
            }
            //判断是否入驻
            $store = AisStore::manageStore($this->user->id);
            if($store){
                return enjson(403,'不需要重复入驻');
            }
            //所在城市
            $citypath = Filter::filter_escape(json_decode($param['citypath'],true));
            if(empty($citypath)){
                return enjson(403,'城市选择失败,请重新选择');
            }
            $imgs  = Filter::filter_escape(json_decode($param['imgs'],true));
            if(empty($imgs)){
                return enjson(403,'请重新选择Logo');
            }
            $data['citycode']          = $citypath[2]['code'];
            $data['citypath']          = ['province' => $citypath[0]['code'],'city' => $citypath[1]['code'],'district' => $citypath[2]['code']];
            $data['member_miniapp_id'] = $this->miniapp_id;
            $data['manage_uid']        = $this->user->id;
            $data['name']              = $param['name'];
            $data['telphone']          = $param['telphone'];
            $data['address']           = $param['address'];
            $data['latitude']          = $param['latitude'];
            $data['longitude']         = $param['longitude'];
            $data['img']               = $imgs[0];
            $data['charge']            = $param['charge'];;
            if(AisStore::create($data)){
                return enjson(200,"成功,下一步申请微信商户号");
            }
            return enjson(403,"提交失败");
        }
    }
}